package com.peng.androidlib.appbase;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.peng.androidlib.R;
import com.peng.androidlib.appwidget.LoadingDialog;
import com.peng.androidlib.mvpbase.BaseModel;
import com.peng.androidlib.mvpbase.BasePresenter;
import com.peng.androidlib.utils.ToastWithImageUtils;

/**
 * 使用举例
 */
//1.mvp模式
//public class SampleFragment extends BaseFragment<NewsChanelPresenter, NewsChannelModel>implements NewsChannelContract.View {
//    @Override
//    public int getLayoutId() {
//        return R.layout.activity_news_channel;
//    }
//
//    @Override
//    public void initPresenter() {
//        mPresenter.setVM(this, mModel);
//    }
//
//    @Override
//    public void initView() {
//    }
//}
//2.普通模式
//public class SampleFragment extends BaseFragment {
//    @Override
//    public int getLayoutId() {
//        return R.layout.activity_news_channel;
//    }
//
//    @Override
//    public void initPresenter() {
//    }
//
//    @Override
//    public void initView() {
//    }
//}

/**
 *  Created by Mr.Q on 2016.07.11:53
 *  描述：
 *      1、管理 Fragment 要显示的 View
 *      2、管理  RxManager,RxManager主要用于 RxJava 及 RxBus 的生命周期的管理
 *      3、对 MVP 模式进行空实现，对不同的界面，使用不同的模式
 */

/***************使用例子*********************/
public abstract class BaseFragment<T extends BasePresenter, E extends BaseModel> extends Fragment {
    protected View rootView;
    public T mPresenter;
    public E mModel;
    public BaseActivity mActivity;
    private Dialog mDialog;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof BaseActivity) {
            mActivity = (BaseActivity) context;
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 初始化DialogFactory
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 建议派生都使用Bundle来传输少量的数据
        Bundle extras = getArguments();
        if (null != extras) {
            handleBundleExtras(extras); //extras不为空，该虚函数才会执行
        }
        if (savedInstanceState != null) {
            handleSavedInstanceState(savedInstanceState);
        }
    }


    protected abstract void handleBundleExtras(@NonNull Bundle extras);

    protected abstract void handleSavedInstanceState(@NonNull Bundle savedInstanceState);


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (getLayoutId() != 0) {
            rootView = inflater.inflate(getLayoutId(), container, false);
        } else {
            rootView = super.onCreateView(inflater, container, savedInstanceState);
        }

        this.initPresenter();
        //如果使用MVP且Presenter初始化成功
        if (mPresenter != null) {
            mPresenter.mContext = this.getActivity();
        }

        return rootView;
    }

    //获取布局文件
    protected abstract int getLayoutId();

    /**
     * 简单页面无需 mvp 就不用管此方法即可,完美兼容各种实际场景的变通
     */
    protected void initPresenter() {

    }

    /**
     * 设置 Fragment
     * @param containerID
     * @param fragment
     */
    public void setFragmentWithReplace(int containerID, Fragment fragment) {
        getChildFragmentManager().beginTransaction().replace(containerID, fragment).commit();
    }


    protected void goneView(@NonNull View... views) {
        for (int i = 0; i < views.length; i++) {
            views[i].setVisibility(View.GONE);
        }
    }

    protected void invisibleView(@NonNull View... views) {
        for (int i = 0; i < views.length; i++) {
            views[i].setVisibility(View.INVISIBLE);
        }
    }

    protected void visibleView(@NonNull View... views) {
        for (int i = 0; i < views.length; i++) {
            views[i].setVisibility(View.VISIBLE);
        }
    }

    public void finishActivity() {
        getActivity().finish();
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
        getActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }


    /**
     * 开启加载进度条
     */
    public void startProgressDialog() {
        mDialog = LoadingDialog.showDialogForLoading(getActivity());
    }

    /**
     * 开启加载进度条
     *
     * @param msg
     */
    public void startProgressDialog(String msg) {
        mDialog = LoadingDialog.showDialogForLoading(getActivity(), msg, true);
    }

    /**
     * 停止加载进度条
     */
    public void stopProgressDialog() {
        if (mDialog != null) {
            mDialog.cancel();
        }
    }


    /**
     * 短暂显示Toast提示(来自String)
     **/
    public void showShortToast(String text) {
        ToastUtils.showShort(text);
    }

    /**
     * 短暂显示Toast提示(id)
     **/
    public void showShortToast(int resId) {
        ToastUtils.showShort(resId);
    }

    /**
     * 长时间显示Toast提示(来自res)
     **/
    public void showLongToast(int resId) {
        ToastUtils.showLong(resId);
    }

    /**
     * 长时间显示Toast提示(来自String)
     **/
    public void showLongToast(String text) {
        ToastUtils.showLong(text);
    }


    public void showToastWithImg(String text, int res) {
        ToastWithImageUtils.showToastWithImg(text, res);
    }

    /**
     * 网络访问错误提醒
     */
    public void showNetErrorTip() {
        ToastWithImageUtils.showToastWithImg(getText(R.string.net_error).toString(), R.mipmap.msg_icon_fail);
    }

    public void showNoNetTip() {
        ToastWithImageUtils.showToastWithImg(getText(R.string.no_net).toString(), R.mipmap.msg_icon_fail);
    }

    public void showNetErrorTip(String error) {
        ToastWithImageUtils.showToastWithImg(error, R.mipmap.msg_icon_fail);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        rootView = null;
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }

        //mRxManager.clear();
    }


}
